package com.xyl.controller;

import com.xyl.commons.ApiResult;
import com.xyl.entity.User;
import com.xyl.services.ActivityService;
import com.xyl.services.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("user")
public class UserController extends BaseController{

    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserService userService;


    @GetMapping("logout")
    public String logout(){
        session.removeAttribute("cur_user");
        return "redirect:/login";
    }

    @PostMapping("login")
    public ModelAndView login(@RequestParam String loginName, @RequestParam String password){
        ModelAndView mav  = new ModelAndView("login");
        if (StringUtils.isEmpty(loginName)) {
            mav.addObject("login_msg","登录账号不能为空");
            return mav;
        }else if(StringUtils.isEmpty(password)){
            mav.addObject("login_msg","密码不能为空");
            return mav;
        }
        User user =  userService.getUserByLoginName(loginName);
        if(ObjectUtils.isEmpty(user)){
            mav.addObject("login_msg","账号未注册");
            return mav;
        }else if(!password.equals(user.getPassword())){
            mav.addObject("login_msg","密码不匹配");
            return mav;
        }
        mav.setViewName("redirect:/activity");
        user.setPassword("");
        session.setAttribute("cur_user",user);
        return mav;
    }

    @PostMapping("register")
    public ModelAndView register(@RequestParam String loginName, @RequestParam String password,@RequestParam String email){
        ModelAndView mav  = new ModelAndView("register");
        User user =  userService.getUserByLoginName(loginName);
        if(!ObjectUtils.isEmpty(user)){
            mav.addObject("register_msg","账号已注册!");
            return mav;
        }
        mav.setViewName("redirect:/login");
        user = new User();
        user.setCreateTime(new Date());
        user.setPassword(password);
        user.setLoginName(loginName);
        user.setEmail(email);
        user.setRealName("---");
        int count = userService.add(user);
        return mav;
    }

    @GetMapping("info")
    public String info(Model model){
        Integer ss = activityService.sumScore(user.getUserId());
        model.addAttribute("score",ss);
        return "info";
    }

    @PostMapping("/modify/info")
    @ResponseBody
    public ApiResult modifyInfo(@RequestParam String realName,@RequestParam String email){
        User user = new User();
        user.setUserId(super.user.getUserId());
        user.setRealName(realName);
        user.setEmail(email);
        user.setUpdateTime(new Date());
        int count = userService.modify(user);
        User curUser = userService.getUserByUserId(super.user.getUserId());
        super.session.removeAttribute("cur_user");
        super.session.setAttribute("cur_user",curUser);
        return resultIsOne(count);
    }

    @PostMapping("/modify/password")
    @ResponseBody
    public ApiResult modifyPassword(@RequestParam String password){
        User user = new User();
        user.setUserId(super.user.getUserId());
        user.setPassword(password);
        int count = userService.modify(user);
        super.session.removeAttribute("cur_user");
        return resultIsOne(count);
    }
}
